#include "main.h"
#include "systick.h"
#include "adc_drv.h"
#include "pwm_drv.h"
#include "rcc_drv.h"
#include "key_drv.h"
#include "lcd_drv.h"
#include "hmi_app.h"
#include "temp_drv.h"
#include "loadctrl_drv.h"

typedef struct
{
    uint8_t run;               // 调度标志，1：调度，0：挂起
    uint16_t timeCount;        // 时间片计数值
    uint16_t timeRload;        // 时间片重载值
    void (*pTaskFuncCb)(void); // 函数指针变量，用来保存业务功能模块函数地址
} TaskComps_t;

static TaskComps_t g_taskComps[] =
{
    // {0, 20, 20, LcdTest},
    {0, 10, 10, HmiTask},
    {0, 100, 100, LcdScan},
    /* 添加业务功能模块 */
};

#define TASK_NUM_MAX (sizeof(g_taskComps) / sizeof(g_taskComps[0]))

static void TaskHandler(void)
{
    for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
    {
        if (g_taskComps[i].run) // 判断时间片标志
        {
            g_taskComps[i].run = 0;      // 标志清零
            g_taskComps[i].pTaskFuncCb(); // 执行调度业务功能模块
        }
    }
}

static void TaskScheduleCb(void)
{
    for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
    {
        if (g_taskComps[i].timeCount)
        {
            g_taskComps[i].timeCount--;
            if (g_taskComps[i].timeCount == 0)
            {
                g_taskComps[i].run = 1;
                g_taskComps[i].timeCount = g_taskComps[i].timeRload;
            }
        }
    }
}

static void DrvInit(void)
{
    RCC_Configuration();
    SystickInit();
    PWMDrvInit();
    KeyDrvInit();
    LcdDrvInit();
}

static void AppInit(void)
{
    TaskScheduleCbReg(TaskScheduleCb);
}

int32_t main(void)
{
    DrvInit();
    AppInit();

    while (1)
    {
        TaskHandler();
    }
}

